<template>
  <div>
    <van-search placeholder="搜索商品" @click="searchGoods">
      <template #left>
        <div @click="onSearch" class="citys">{{ city }}</div>
      </template>
    </van-search>
    <div id="allmap"></div>
    <!-- 轮播图 -->
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
      <van-swipe-item v-for="item in banner" :key="item.id"
        ><img :src="item.image_url" alt="" class="swiperimg"
      /></van-swipe-item>
    </van-swipe>
    <!-- gird宫格 -->
    <van-grid :column-num="5">
      <van-grid-item
        :icon="item.icon_url"
        :text="item.name"
        v-for="(item, index) in channel"
        :key="index"
        @click="getCate(item.id)"
      />
    </van-grid>
    <!-- barand区域 -->
    <div class="head">品牌制造商直供</div>
    <div class="content">
      <div
        class="brand"
        v-for="item in brandList"
        :key="item.id"
        @click="goInfo(item.id)"
      >
        <div class="brands">
          <p>{{ item.name }}</p>
          <p>{{ item.floor_price }}元起</p>
        </div>
        <img :src="item.new_pic_url" alt="" />
      </div>
    </div>
    <!-- 新品首发 -->
    <div class="news">
      <div class="top">
        <p class="new-goods">新品首发</p>
        <p class="new-all" @click="getGoods">查看全部</p>
      </div>
    </div>
    <div class="list">
      <ul>
        <li v-for="item in newGoods" :key="item.id" @click="goGoods(item.id)">
          <img :src="item.list_pic_url" alt="" />
          <p class="name">{{ item.name }}</p>
          <p class="brief">{{ item.goods_brief }}</p>
          <p class="price">￥{{ item.retail_price }}</p>
        </li>
      </ul>
    </div>
    <!-- 人气推荐 -->
    <div class="news-hot">
      <div class="top">
        <p class="new-goods-hot">人气推荐 <span></span> 好物精选</p>
        <p class="new-all-hot" @click="getHot">查看全部</p>
      </div>
    </div>
    <div class="list">
      <ul>
        <li v-for="item in hotGoods" :key="item.id" @click="goGoods(item.id)">
          <img :src="item.list_pic_url" alt="" />
          <p class="name">{{ item.name }}</p>
          <p class="brief">{{ item.goods_brief }}</p>
          <p class="price">￥{{ item.retail_price }}</p>
        </li>
      </ul>
    </div>
    <!-- 专题部分 -->
    <div class="topic">
      <div class="topic-top">专题精选</div>
      <div class="toppic-list">
        <ul>
          <li v-for="item in topicList" :key="item.id" @click="go(item.id)">
            <img :src="item.item_pic_url" alt="" />
            <div class="botm">
              <div class="texts">
                <p class="name">{{ item.title }}</p>
                <p class="brief">{{ item.subtitle }}</p>
              </div>
              <p class="price">{{ item.price_info }}元起</p>
            </div>
          </li>
        </ul>
      </div>
    </div>
    <!-- 居家好物 -->
    <div class="newcategory" v-for="item in newCategoryList" :key="item.id">
      <div class="newcategory-top">{{ item.name }}好物</div>
      <div class="sublist">
        <div
          class="sublist-item"
          v-for="items in item.goodsList"
          :key="items.id"
          @click="goGoods(items.id)"
        >
          <img :src="items.list_pic_url" alt="" />
          <p class="sublist-name">{{ items.name }}</p>
          <p class="sublist-price">￥{{ items.retail_price }}</p>
        </div>
        <div class="sublist-item" @click="getCate(item.id)">
          <div class="last">
            <p>{{ item.name }}好物</p>
            <span class="icon"></span>
          </div>
        </div>
      </div>
    </div>
    <tab></tab>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { index } from "@/api/home";
import { Toast } from "vant";
// import Navbar from "../../../components/Navbar";
import tab from "../all/rt.vue";
export default {
  props: {},
  data() {
    return {
      city: "",
      banner: [],
      channel: [],
      brandList: [],
      newGoods: [],
      hotGoods: [],
      topicList: [],
      newCategoryList: [],
    };
  },
  computed: {
    ...mapState(["cityInfo"]),
  },
  created() {
    index().then((res) => {
      // console.log(res);
      this.banner = res.banner;
      this.channel = res.channel;
      this.brandList = res.brandList;
      this.newGoods = res.newGoods;
      this.hotGoods = res.hotGoods;
      this.topicList = res.topicList;
      this.newCategoryList = res.newCategoryList;
    });
  },
  mounted() {
    if (this.cityInfo.city) {
      // console.log("使用vuex中的数据", this.cityInfo.city);
      this.city = this.cityInfo.title;
      // console.log("使用vuex中的数据", this.city);
    } else {
      var _this = this;
      var map = new BMap.Map("allmap"); // 创建Map实例
      map.centerAndZoom(new BMap.Point(116.404, 39.915), 11); // 初始化地图,设置中心点坐标和地图级别
      //添加地图类型控件

      var geolocation = new BMap.Geolocation();
      geolocation.getCurrentPosition(
        function (r) {
          if (this.getStatus() == BMAP_STATUS_SUCCESS) {
            // console.log(r)
            _this.city = r.address.city;
          } else {
            alert("failed" + this.getStatus());
          }
        },
        { enableHighAccuracy: true }
      );
    }
  },
  watch: {},
  methods: {
    onSearch() {
      this.$router.push("/home/city");
    },
    searchGoods() {
      this.$router.push("/home/search");
    },
    goInfo(id) {
      this.$router.push({ path: "/home/brandinfo", query: { ids: id } });
    },
    getGoods() {
      this.$router.push("/home/news");
    },
    getHot() {
      this.$router.push("/home/hot");
    },
    go(id) {
      this.$router.push({ path: "/topic/details", query: { ids: id } });
    },
    getCate(id) {
      this.$router.push({ path: "/home/cate", query: { ids: id } });
    },
    // 跳转商品详情
    goGoods(id) {
      this.$router.push({ path: "/home/goods", query: { ids: id } });
    },
  },
  components: {
    tab: tab,
  },
};
</script>

<style lang="scss" scoped>
.citys {
  max-width: 70px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.swiperimg {
  display: block;
  width: 100%;
}
.head {
  text-align: center;
  padding: 20px 0;
  margin-top: 8px;
  background-color: #fff;
}
.content {
  width: 365px;
  margin: 0px auto;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  background-color: #fff;
}
.brand {
  width: 180px;
  height: 116px;
  margin-bottom: 5px;
  position: relative;

  img,
  .brands {
    width: 100%;
    height: 100%;
  }
  .brands {
    position: absolute;
    top: 0px;
    left: 0px;

    p {
      text-align: left;
      font-weight: 500;
      margin-left: 5px;
    }
  }
}
.news,
.news-hot {
  width: 100%;
  height: 122px;
  background-size: 100%, 100%;
  margin-top: 12px;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
}
.news {
  background: url("")
    no-repeat;
}
.news-hot {
  background: url("")
    no-repeat;
}
.new-goods {
  color: rgb(140, 155, 174);
  font-size: 16px;
}
.new-goods-hot {
  color: #b1a279;
  font-size: 16px;
  span {
    width: 2px;
    height: 2px;
    font-size: 7px;
    display: inline-block;
    vertical-align: middle;
    background: #b1a279;
  }
}
.new-all,
.new-all-hot {
  width: 90px;
  height: 24px;
  line-height: 24px;
  margin: 14px auto 0px;
  font-size: 12px;
}
.new-all-hot {
  background: #f4e9cb;
  color: #b1a279;
}
.new-all {
  background: rgb(216, 228, 240);
  color: rgb(140, 155, 174);
}
.list {
  margin-top: 10px;
  background: #fff;
  width: 100%;
  ul {
    width: 100%;
    white-space: nowrap;
    overflow: auto;
    li:nth-child(n + 2) {
      border-left: 1px solid #f4f4f4;
    }
    li {
      width: 140px;
      height: 208px;
      margin: 3px 3px 12px;
      display: inline-block;
      img {
        width: 140px;
        height: 140px;
      }
      p {
        width: 94%;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        margin-top: 0.10667rem;
        text-indent: 1em;
      }
      .name {
        font-size: 15px;
        font-weight: 700;
      }
      .brief {
        color: #8a8a8a;
        font-size: 12px;
      }
      .price {
        color: #9c3232;
        font-size: 12px;
        text-align: left;
      }
    }
  }
}
.topic {
  margin-top: 10px;
  background-color: #fff;
  .topic-top {
    text-align: center;
    padding: 18px;
    vertical-align: middle;
  }
  .toppic-list {
    white-space: nowrap;
    overflow: auto;
    ul {
      width: 100%;
      white-space: nowrap;
      overflow: auto;
      li {
        display: inline-block;
        width: 7.66667rem;
        margin-left: 0.33333rem;
        img {
          display: block;
          width: 7.66667rem;
          height: 4.33333rem;
          border-radius: 0.13333rem;
        }
        p {
          width: 94%;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
          margin-top: 5px;
        }
        .botm {
          display: flex;
          justify-content: space-between;
          margin-bottom: 20px;
          width: 100%;
        }
        .texts {
          width: 90%;
          text-align: left;
        }
        .name {
          font-size: 15px;
          font-weight: 700;
          text-align: left;
        }
        .brief {
          width: 90%;
          color: #8a8a8a;
          font-size: 12px;
          text-align: left;
        }
        .price {
          margin-top: 5px;
          color: #9c3232;
          font-size: 12px;
        }
      }
    }
  }
}
.newcategory {
  margin-top: 10px;
  padding: 0 5px 5px 5px;
  .newcategory-top {
    padding: 12px 0;
    text-align: center;
  }
  .sublist {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    width: 365px;
    margin: 0 auto;
    .sublist-item {
      width: 180px;
      background: #fff;
      margin-bottom: 5px;
      padding-bottom: 5px;
      img {
        display: block;
        width: 150px;
        height: 150px;
        margin: 0 auto;
      }

      .sublist-name {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        width: 98%;
        text-align: left;
        margin-bottom: 0.06667rem;
        text-indent: 1em;
      }
      .sublist-price {
        color: #9c3232;
        text-align: left;
        margin-bottom: 0.06667rem;
        text-indent: 1em;
      }
      .last {
        width: 150px;
        height: 150px;
        margin: 0 auto;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        flex-wrap: wrap;
        p {
          height: 16px;
          width: 100%;
          line-height: 16px;
          color: #333;
          font-size: 16px;
          text-align: center;
        }
        span {
          display: inline-block;
          width: 35px;
          height: 35px;
          background: url()
            no-repeat;
          background-size: 100% 100%;
          margin-top: 30px;
        }
      }
    }
  }
}
</style>
